package com.lipy.tools.entitymapping;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.Named;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description 转换接口
 * @Date 2020/11/25 17:22
 * @Author by lipye
 * url https://mapstruct.org/documentation/stable/reference/html/#mapping-with-builders 官网文档
 */

@Mapper
        //(componentModel = "spring")
public interface StudentToVO {

    // 默认只转换同名的
    // studentVO = StudentVO{name='null', stuNumber='ABC123', gender='1'}
    @Mappings({})
    StudentVO toStudentVO(Student student);

    // studentVO = StudentVO{name='lili', stuNumber='ABC123', gender='男'}
    @Mappings({
            // 属性名不一致 需要指定
            @Mapping(source = "stuName",target = "name"),
            @Mapping(source = "gender",target = "gender",qualifiedByName = "toGenderStr")
    })
    StudentVO toStudentVO2(Student student);

    @Named("toGenderStr")
    default public String toGenderStr(Integer gender){
        String s = gender == 1 ? "男" : "女";
        return  s;
    }

    // qualifiedByName 和 @Named 组合使用 可以进行复杂类型转换
    @Mappings({
            @Mapping(source = "students",target = "students" ,qualifiedByName = "toStudentVOList")
    })
    GradeVO convert(Grade grade);

    @Named("toStudentVOList")
    default List<StudentVO> toStudentVOList(List<Student> students){
        List<StudentVO> studentVOList = new ArrayList<>(students.size());
        for (int i = 0; i < students.size(); i++) {
            studentVOList.add(toStudentVO2(students.get(i)));
        }
        return studentVOList;
    }
    // builde 注意版本问题 版本低可能不支持 source 的get方法要有
    @Mappings({
    })
    PersonVo convertPerson(Person person);
}
